package com.personalkeeper;

import java.util.ArrayList;

import android.content.Context;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.personalkeeper.PersonalKeeper.Utils;

public class PathHolder {

    public PathHolder(PolylineLayer polyline, MarkersLayer markers, Context context) {
        this.path = polyline;
        this.markersOnPath = markers;
        this.context = context;
    }
    private PolylineLayer path;
    private MarkersLayer markersOnPath;
    private Context context;
    private int pointsOnPath = 0;
    private boolean isReset = true;

    public void reset(boolean reset) {
        this.isReset = reset;
    }
    public void clear() {
        path.clear();
        markersOnPath.clear();
        pointsOnPath = 0;
    }
    public void addPoint(Location location) {
        if (isReset)
            clear();
        if (markersOnPath.size() != 0) {
            ((Marker)markersOnPath.getItem(markersOnPath.size() - 1)).setType(Marker.TYPE_PASSED_POINT);
        }
        if (location != null) {
            GeoPoint currentGeoPoint = Utils.geoPointFromLocation(location);

            markersOnPath.addOverlay(
                Utils.createMarkerFromLocation(context, location, Marker.TYPE_CURRENT_POINT));
            path.DrawLineTo(currentGeoPoint);
            pointsOnPath++;
        }
        isReset = false;
    }
    public void addPoint(Marker item) {
        if (markersOnPath.size() != 0) {
            ((Marker)markersOnPath.getItem(markersOnPath.size() - 1)).setType(Marker.TYPE_PASSED_POINT);
        }
        markersOnPath.addOverlay(item);
        path.DrawLineTo(item.getPoint());
        item.setType(Marker.TYPE_PASSED_POINT);
        isReset = false;
    }
    public void addRange(ArrayList<Marker> items) {
        for (Marker marker: items) {
            path.DrawLineTo(marker.getPoint());
            markersOnPath.addOverlay(marker);
        }
    }
    public void updateLastPoint() {
        if (markersOnPath.size() != 0) {
            ((Marker)markersOnPath.getItem(markersOnPath.size() - 1)).setType(Marker.TYPE_PASSED_POINT);
        }
    }
    public boolean isReset() {
        return isReset;
    }
    public int PointsOnPath() {
        return this.pointsOnPath;
    }
    public ArrayList<Marker> getItems() {
        return markersOnPath.getItems();
    }
}
